/**
 * @param {string} S
 * @return {string}
 */
var removeDuplicates = function(S) {
  const toHeavy = str => {
    return /(\w)\1/g.test(str) ? toHeavy(str.replace(/(\w)\1/g, "")) : str
  }

  return toHeavy(S)
}

console.log(removeDuplicates("abbaca"))
